<?php

namespace App\Http\Controllers;

use App\Models\CaseModel;
use App\Models\Configs;
use App\Models\Domain;
use App\Models\Layer;
use App\Models\News;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        $banners = Configs::query()->where('type', '=', 'banner')->pluck('value')->toArray();
        $domains = Domain::query()->limit(6)->get()->toArray();
        $cases = CaseModel::query()->limit(6)->get()->toArray();
        $configs = Configs::query()->whereIn('key', ['about_1'])->get()->keyBy('key')->toArray();
        $layers = Layer::query()->limit(6)->get()->toArray();
        $news = News::query()->limit(6)->orderBy('id', 'desc')->get()->toArray();
        foreach ($news as &$new) {
            if (isset($new['created_at'])) {
                $date = strtotime($new['created_at']);
                $new['month_format'] = date('Y-m', $date);
                $new['day'] = date('d', $date);
            }
        }
        unset($new);
        $params = [
            'banners' => $banners,
            'domains' => $domains,
            'case' => $cases,
            'about' => $configs['about_1']['value'],
            'layers' => $layers,
            'news' => $news,
            'bottom' => $this->getBottomConfig()
        ];
        return view('index', $params);
    }

    public function about()
    {
        $configs = Configs::query()->whereIn('key', ['about_1', 'about_2'])->get()->keyBy('key')->toArray();
        $honours = Configs::query()->where('type', 'honour')->get()->pluck('value')->toArray();
        return view('template.about', ['about_1' => $configs['about_1'], 'about_2' => $configs['about_2'], 'honours' => $honours, 'bottom' => $this->getBottomConfig()]);
    }

    public function domain()
    {
        return view('template.domain');
    }

    public function domainDetail(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            $domain = Domain::query()->first();
        }else{
            $domain = Domain::query()->where('id', $id)->first();
        }
        $list = Domain::query()->select(['id','name'])->get()->toArray();
        $phone = Configs::query()->where('key','phone')->first();

        return view('template.domain_detail', ['info' => $domain->toArray(), 'list' => $list, 'phone' => $phone, 'bottom' => $this->getBottomConfig() ]);
    }

    public function layer()
    {
        $items = Layer::paginate(8);
        return view('template.layer', ['items' => $items, 'bottom' => $this->getBottomConfig()]);
    }

    public function news()
    {
        $items = News::paginate(6);

        return view('template.news', ['items' => $items, 'bottom' => $this->getBottomConfig()]);
    }

    public function newDetails(Request $request)
    {
        $id = $request->input('id');
        $news = News::query()->where('id', $id)->first();
        if (empty($news)) {
            return redirect('/');
        }
        $next = News::query()->where('id', ">", $id)->select(['id', 'title'])->first();
        $last = News::query()->where('id', "<", $id)->orderBy('id', 'desc')->select(['id', 'title'])->first();
        // 增加浏览量
        $news->increment('watch');
        return view('template.news_detail', ['info' => $news->toArray(), 'next' => $next, 'last' => $last, 'bottom' => $this->getBottomConfig()]);
    }

    public function contact()
    {
        $config = Configs::query()->whereIn('key',['address', 'email', 'phone'])->get()->keyBy('key')->toArray();

        $phone = $config['phone']['value'];
        $email = $config['email']['value'];
        $address = $config['address']['value'];

        $bottom = $this->getBottomConfig();
        return view('template.contact', compact('email', 'phone', 'address' , 'bottom'));
    }

    public function case()
    {
        $items = CaseModel::paginate(6);
        return view('template.case', ['items' => $items, 'bottom' => $this->getBottomConfig()]);
    }

    public function caseDetail(Request $request)
    {
        $id = $request->input('id');
        $case = CaseModel::query()->where('id', $id)->first();
        if (empty($case)) {
            return redirect('/');
        }
        $next = CaseModel::query()->where('id', ">", $id)->select(['id', 'title'])->first();
        $last = CaseModel::query()->where('id', "<", $id)->orderBy('id', 'desc')->select(['id', 'title'])->first();
        // 增加浏览量
        $case->increment('watch');
        return view('template.case_detail', ['info' => $case->toArray(), 'next' => $next, 'last' => $last, 'bottom' => $this->getBottomConfig()]);
    }
}
